package cn.org.wifit.imserver.zookeeper.handle;

import cn.org.wifit.core.zookeeper.NodeWriteDistributedTransactionWatcherHandle;
import cn.org.wifit.imserver.boot.BootStartup;
import cn.org.wifit.imserver.zookeeper.manager.ZookeeperIMServerWriteManager;

/**
 * 服务器启动的事件的业务处理类
 * @author yinwenjie
 */
public class IMServerBootWatcherHandle implements NodeWriteDistributedTransactionWatcherHandle {
	
	private ZookeeperIMServerWriteManager imServerWriteManager;
	
	/* (non-Javadoc)
	 * @see cn.org.wifit.core.zookeeper.NodeWriteDistributedTransactionWatcherHandle#doHandle(java.lang.String, java.lang.String)
	 */
	@Override
	public void doHandle(String mySequence, String eventFullpath) throws Exception {
		/*
		 * 启动的过程，主要是在zookeeper的IMServer节点上注册表示自己的子节点信息
		 * */
		//获取-U -T -P
		String udp = BootStartup.LOCAL_Environment.get("-U");
		String tcp = BootStartup.LOCAL_Environment.get("-T");
		String ip = BootStartup.LOCAL_Environment.get("-P");
		
		boolean isok = false;
		try {
			//先在本地节点初始化
			isok = this.imServerWriteManager.createLocalServerNodeTree();
			//然后再向zookeeper远程节点注册
			if(isok) {
				this.imServerWriteManager.createZookeeperNode(ip , Integer.valueOf(udp) , Integer.valueOf(tcp));
			}
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ZookeeperIMServerWriteManager getImServerWriteManager() {
		return imServerWriteManager;
	}

	public void setImServerWriteManager(
			ZookeeperIMServerWriteManager imServerWriteManager) {
		this.imServerWriteManager = imServerWriteManager;
	} 
}